package com.tyust.web;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tyust.entity.PageUtils;
import com.tyust.entity.ResponseDto;
import com.tyust.entity.Staff;
import com.tyust.entity.User;
import com.tyust.service.StaffService;
import com.tyust.util.commons.CommonUtil;
import com.tyust.util.commons.DateUtil;

@Controller
@RequestMapping("/staff")
public class StaffController {
	@Autowired
	private StaffService staffService;
	
	@RequestMapping("/add.do")
	@ResponseBody
	public ResponseDto add(@RequestBody Staff staff) throws SQLException{
		staff.setStaffNo(CommonUtil.uuid());
		staffService.add(staff);
		return ResponseDto.ok();
	}
	@RequestMapping("/searchStaffInfo.do")
	@ResponseBody
	public ResponseDto searchStaffInfo(Staff staff){
		PageUtils page = staffService.searchStaffInfo(staff);
		return ResponseDto.ok().put("page", page);
	}
	
	@RequestMapping("/updateStaffInfo.do")
	@ResponseBody
	public ResponseDto updateStaffInfo(@RequestBody Staff staff){
		staffService.updateStaffInfo(staff);
		return ResponseDto.ok();
	}
	
	@RequestMapping("/deleteByNo.do")
	@ResponseBody
	public ResponseDto deleteByNo(@RequestBody String[] staffNos){
		for(String staffNo:staffNos) {
			staffService.deleteByNo(staffNo);
		}
		return ResponseDto.ok();
	}
	
	@RequestMapping("/getStaffByNo.do")
	@ResponseBody
	public ResponseDto getStaffByNo(String staffNo){
		Staff staff=new Staff();
		staff.setStaffNo(staffNo);
		Staff result = staffService.getStaffByNo(staff);
		result.setEntryDateStr(DateUtil.dateTostr(result.getEntryDate()));
		result.setBirthDateStr(DateUtil.dateTostr(result.getBirthDate()));
		return ResponseDto.ok().put("result", result);
	}
}
